<script>
import { GetConfig, GetAppVersion, GetEnum, GetSysCoin, gettime } from '@/api/Ctrls.js';
import permission from './utils/permission.js';
import Updater from '@/utils/upgrade.js';
import cookie from '@/utils/auth.js';
import { device_getOAID } from '@/utils/plusTool.js';
export default {
	data() {
		return {
			networkStatus: 'checking'
		};
	},
	onLaunch(e) {
	},
	onShow() {
		this.checkNetwork();
		permission();
		//基本配置-
		gettime();
		GetConfig();
		GetSysCoin();
		GetEnum();
		this.getSystemInfoFn();
	},
	methods: {
		// 系统信息获取
		async getSystemInfoFn() {
			// #ifdef APP-PLUS
			// const oaid = await device_getOAID();
			// const oaid = await device_getOAID();
			// #endif
			//#ifdef H5
			// const oaid = '864297060331813';
			// const oaid = '864297060331814';
			// const oaid = '860594043770305';
			// #endif
			// console.log('oaid===>', oaid);
			// store.commit('general/set_uniqueID', oaid);
			// cookie.set('uniqueID', oaid);
			// const system = uni.getSystemInfoSync();
			// let { deviceId } = uni.getSystemInfoSync();
			// console.log('system', system);
			// console.log('deviceId', deviceId);
			// let ua = navigator.userAgent.toLowerCase();
			// if (ua.match(/MicroMessenger/i) == 'micromessenger' || system.ua.match(/MicroMessenger/i) == 'micromessenger') {
			// 	console.log('微信内');
			// } else {
			// 	console.log('微信外');
			// }
			// switch (system.platform.toLowerCase()) {
			// 	case 'android':
			// 		console.log('安卓');
			// 		break;
			// 	case 'ios':
			// 		console.log('安卓');
			// 		break;
			// 	default:
			// 		break;
			// }
		},
		async checkNetwork() {
			console.log('检测网络开始')
			this.networkStatus = 'checking';
			const result = await this.comprehensiveNetworkCheck();
			console.log('检测网络开始结果===>',result)
			switch (result.status) {
				case 'normal':
					this.networkStatus = 'connected';
					break;
				case 'blocked':
					this.networkStatus = 'blocked';
					uni.showModal({
						title: '网络提示',
						content: '检测到网络可能被禁用，请检查网络设置',
						showCancel: false
					});
					break;
				case 'no_network':
					this.networkStatus = 'no_network';
					uni.showModal({
						title: '网络提示',
						content: '设备无网络连接,请检查网络设置',
						showCancel: false
					});
					break;
				default:
					this.networkStatus = 'error';
			}
		},
		// 综合网络检测
		async comprehensiveNetworkCheck() {
			try {
				// 先检查网络类型
				const networkType = await this.checkNetworkType();
				if (networkType.connected === false) {
					return { status: 'no_network', message: '设备无网络连接' };
				} 
				// 再检查实际网络请求
				const requestResult = await this.checkNetworkStatus();

				if (requestResult.connected) {
					return { status: 'normal', message: '网络连接正常' };
				} else {
					return { status: 'blocked', message: '网络可能被禁用或限制' };
				}
			} catch (error) {
				return { status: 'error', message: '检测失败: ' + error.message };
			}
		},

		// 检测网络连接状态
		checkNetworkStatus() {
			return new Promise((resolve) => {
				uni.request({
					url: 'https://www.baidu.com', // 使用一个稳定的网站
					method: 'GET',
					timeout: 5000,
					success: () => {
						resolve({ connected: true, message: '网络连接正常' });
					},
					fail: (err) => {
						if (err.errMsg.includes('timeout') || err.errMsg.includes('fail')) {
							resolve({ connected: false, message: '网络连接失败，可能被禁用' });
						} else {
							resolve({ connected: false, message: err.errMsg });
						}
					}
				});
			});
		},
		// 检测网络类型
		checkNetworkType() {
			return new Promise((resolve) => {
				uni.getNetworkType({
					success: (res) => {
						if (res.networkType === 'none') {
							resolve({ connected: false, message: '无网络连接' });
						} else {
							// 有网络类型，但可能被禁用，需要进一步检测
							resolve({ connected: 'unknown', networkType: res.networkType });
						}
					},
					fail: () => {
						resolve({ connected: false, message: '无法获取网络状态' });
					}
				});
			});
		}
	}
};
</script>

<style lang="scss">
/* #ifndef APP-PLUS-NVUE */
@import './styles/color.css';
/*每个页面公共css */
@import './styles/icon/iconfont.css';
@import './styles/base.scss';
@import './styles/index.css';
@import './styles/animation.css';
@import './styles/font/font.css';

page {
	background: var(--bg);
	height: 100%;
	// -webkit-user-select: text;
}

uni-toast .uni-toast__content {
	padding: 0 10rpx;
	font-size: 24rpx;
}

/* #endif */
/* ==================
	         导航栏
	 ==================== */

.nav {
	white-space: nowrap;
}

::-webkit-scrollbar {
	display: none;
}

.nav .cu-item {
	height: 90upx;
	display: inline-block;
	line-height: 90upx;
	margin: 0 10upx;
	padding: 0 20upx;
}

.nav .cu-item.cur {
	border-bottom: 4upx solid;
}
</style>
